package com.jstyle.devicenametest.db;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.room.Room;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.jstyle.devicenametest.db.dao.ExitDao;
import com.jstyle.devicenametest.db.dao.VibDao;


public class DataDbHelper {
    private static final String TAG = "DemoDbHelper";
    private static DataDbHelper instance;
    private Context mContext;
    private String currentUser;
    private AppDataBase mDatabase;
    static final Migration MIGRATION_1_2 = new Migration(1,2){

        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //创建的时候一定要加PRIMARY KEY NOT NULL，否则索引会为null
           database.execSQL("CREATE TABLE IF NOT EXISTS `exitaddress` (`address` TEXT NOT NULL, PRIMARY KEY(`address`))");
           database.execSQL("CREATE TABLE IF NOT EXISTS `vibaddress` (`address` TEXT NOT NULL, PRIMARY KEY(`address`))");
        }
    };

    private final MutableLiveData<Boolean> mIsDatabaseCreated = new MutableLiveData<>();

    private DataDbHelper(Context context){
        this.mContext = context.getApplicationContext();
    }

    public static DataDbHelper getInstance(Context context) {
        if(instance == null) {
            synchronized (DataDbHelper.class) {
                if(instance == null) {
                    instance = new DataDbHelper(context);
                }
            }
        }
        return instance;
    }
    public static DataDbHelper getInstance() {
        return instance;
    }
    /**
     * 初始化数据库
     * @param
     */
    public void initDb() {
//        if(currentUser != null) {
//            if(TextUtils.equals(currentUser, user)) {
//                EMLog.i(TAG, "you have opened the db");
//                return;
//            }
//            closeDb();
//        }
//        this.currentUser = user;
      //  String userMd5 = MD5.encrypt2MD5(user);
        // 以下数据库升级设置，为升级数据库将清掉之前的数据，如果要保留数据，慎重采用此种方式
        // 可以采用addMigrations()的方式，进行数据库的升级
        String dbName = String.format("em_%1$s.db", "jstyle");
        ;
        mDatabase = Room.databaseBuilder(mContext, AppDataBase.class, dbName)
                        .allowMainThreadQueries()
                        .addMigrations(MIGRATION_1_2)

                        .build();
        mIsDatabaseCreated.postValue(true);
    }

    public LiveData<Boolean> getDatabaseCreatedObservable() {
        return mIsDatabaseCreated;
    }

    /**
     * 关闭数据库
     */
    public void closeDb() {
        if(mDatabase != null) {
            mDatabase.close();
            mDatabase = null;
        }
        currentUser = null;
    }



    public ExitDao getExitDao() {
        if(mDatabase != null) {
            return mDatabase.exitDao();
        }
        Log.i(TAG, "get bindContactDao failed, should init db first");
        return null;
    }
    public VibDao getVibDao() {
        if(mDatabase != null) {
            return mDatabase.vibDao();
        }
        Log.i(TAG, "get bindContactDao failed, should init db first");
        return null;
    }


}
